package com.fernfx.xingtan.my.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.baidu.mapapi.model.LatLng;
import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.base.BaseView;
import com.fernfx.xingtan.common.network.IRequestCallback;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.my.contract.MySettingContract;
import com.fernfx.xingtan.my.entity.QiniuEntity;
import com.fernfx.xingtan.my.entity.UpdateAvatarEntity;
import com.fernfx.xingtan.my.entity.UserInfoEntity;
import com.fernfx.xingtan.my.model.MySettingModel;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.ToastUtil;
import com.fernfx.xingtan.view.dialog.ActionSheetDialog;

import java.util.Map;

/**
 * Created by admin on 2018/8/13.
 */

public class MySettingPresenter<P extends MySettingContract.View> implements MySettingContract.Presenter {
    private P P;
    //model
    private MySettingContract.Model model;

    @Override
    public void init(BaseView P) {
        this.P = (P) P;
        this.model = new MySettingModel();
    }

    @Override
    public void request(Map<String, Object> requestArgsMap) {
        P.getActivity().showLoading();
        model.request(requestArgsMap, new IRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                String message = result.getMessage();
                if (TextUtils.isEmpty(message)) {
                    onNetworkError(result.getMessage(),500);onNetworkError(result.getMessage(),500);
                    return;
                }
                UserInfoEntity userInfoEntity = FastJsonUtil.fromBean(message, UserInfoEntity.class);
                if (null != userInfoEntity && OtherUtil.checkRequestStatus(userInfoEntity.getSuccess(),userInfoEntity.getStatus())) {
                    P.showUserInfo(userInfoEntity);
                    //获取七牛云存储token
                    getQiniuToken();
                }else {
                    P.getActivity().hideLoading();
                    ToastUtil.showCentertoast(userInfoEntity.getMsg());
                }
            }

            @Override
            public void onFailure(String errorMessage, int errorCode) {
                P.getActivity().hideLoading();
                if (TextUtils.isEmpty(errorMessage)) {
                    onNetworkError(errorMessage,errorCode);
                    return;
                }

                final BaseEntity baseEntity = FastJsonUtil.fromBean(errorMessage, BaseEntity.class);
                if (null != baseEntity && !TextUtils.isEmpty(baseEntity.getMsg())){
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }

            @Override
            public void onNetworkError(String errorMessage, int errorCode) {
                P.getActivity().hideLoading();
                ToastUtil.showCentertoast(R.string.network_error);
            }
        });
    }

    @Override
    public void detachView() {
        if (null != model) {
            model = null;
        }
    }

    @Override
    public void showUserLogoDialog(Context context) {
        new ActionSheetDialog(context)
                .builder()
                .setCancelable(false)
                .setCanceledOnTouchOutside(false)
                .addSheetItem("打开相机", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                P.autoObtainCameraPermission();
                            }
                        }).addSheetItem("打开相册", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        P.autoObtainStoragePermission();
                    }
                }).show();
    }

    @Override
    public void getQiniuToken() {
        model.getQiniuToken(new IRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                P.getActivity().hideLoading();
                String message = result.getMessage();
                if (TextUtils.isEmpty(message)) {
                    onNetworkError(result.getMessage(),500);onNetworkError(result.getMessage(),500);
                    return;
                }
                QiniuEntity qiniuEntity = FastJsonUtil.fromBean(message, QiniuEntity.class);
                if (null != qiniuEntity && OtherUtil.checkRequestStatus(qiniuEntity.getSuccess(),qiniuEntity.getStatus())) {
                    P.setQiniuToken(qiniuEntity.getMsg());
                }else {
                    P.getActivity().hideLoading();
                    ToastUtil.showCentertoast(qiniuEntity.getMsg());
                }
            }

            @Override
            public void onFailure(String errorMessage, int errorCode) {
                P.getActivity().hideLoading();
                if (TextUtils.isEmpty(errorMessage)) {
                    onNetworkError(errorMessage,errorCode);
                    return;
                }

                final BaseEntity baseEntity = FastJsonUtil.fromBean(errorMessage, BaseEntity.class);
                if (null != baseEntity && !TextUtils.isEmpty(baseEntity.getMsg())){
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }

            @Override
            public void onNetworkError(String errorMessage, int errorCode) {
                P.getActivity().hideLoading();
                ToastUtil.showCentertoast(R.string.network_error);
            }
        });
    }

    @Override
    public void updateAvatar(Map<String,Object> requestArgsMap) {
        P.getActivity().showLoading();
        model.updateAvatar(requestArgsMap, new IRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                P.getActivity().hideLoading();
                String message = result.getMessage();
                if (TextUtils.isEmpty(message)) {
                    onNetworkError(result.getMessage(),500);onNetworkError(result.getMessage(),500);
                    return;
                }
                //判断图片上传是否成功
                final UpdateAvatarEntity updateAvatarEntity = FastJsonUtil.fromBean(message, UpdateAvatarEntity.class);
                if (!(null != updateAvatarEntity && OtherUtil.checkRequestStatus(updateAvatarEntity.getSuccess(),updateAvatarEntity.getStatus()))) {
                    ToastUtil.showCentertoast(updateAvatarEntity.getMsg());
                }else {
                    P.updateAvatar(updateAvatarEntity.getObj());
                    ToastUtil.showCentertoast("上传成功");
                }
            }

            @Override
            public void onFailure(String errorMessage, int errorCode) {
                P.getActivity().hideLoading();
                if (TextUtils.isEmpty(errorMessage)) {
                    onNetworkError(errorMessage,errorCode);
                    return;
                }

                final BaseEntity baseEntity = FastJsonUtil.fromBean(errorMessage, BaseEntity.class);
                if (null != baseEntity && !TextUtils.isEmpty(baseEntity.getMsg())){
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }

            @Override
            public void onNetworkError(String errorMessage, int errorCode) {
                P.getActivity().hideLoading();
                ToastUtil.showCentertoast(R.string.network_error);
            }
        });
    }
}
